package com.njlime.sys.controller;

import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import com.njlime.sys.entities.Menu;
import com.njlime.sys.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Dean on 2017-7-25.
 */
@Controller
@RequestMapping(value = "sys/menu")
public class MenuController extends BaseController {
    @Resource
    private MenuService menuService;


    //-------------------控制器--------------------

    /**
     * 跳转热点管理页面
     * @return
     */
    @RequestMapping(value = "menuManage")
    public ModelAndView menuManage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/terrace/menu/index");
        return modelAndView;
    }

    //------------------接口-----------------

    /**
     * 获取菜单列表分页
     *
     * @param request
     * @param menu
     * @param pageBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "menuList", method = RequestMethod.GET)
    public String menuList(HttpServletRequest request, Menu menu, PageBean pageBean) {
        try {
            if (pageBean == null) {
                pageBean = new PageBean();
            }
            if(menu == null){
                menu = new Menu();
            }
            return toJsonListData(menuService.list(pageBean, menu));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 获取全部菜单--根据当前用户
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "allMenu", method = RequestMethod.GET)
    public String allMenu(HttpServletRequest request) {
        try {
            return toJsonListData(menuService.getUserMenu(GlobalValue.YES));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 添加菜单
     *
     * @param request
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, Menu menu) {
        try {
            ValidateService.valid(menu);
            menuService.addMenu(menu);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 获取菜单信息
     *
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "menuInfo", method = RequestMethod.GET)
    public String menuInfo(HttpServletRequest request, Integer id) {
        try {
            return toJsonSuccessData(menuService.getById(id));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 编辑菜单
     *
     * @param request
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public String edit(HttpServletRequest request, Menu menu) {
        try {
            menuService.editMenu(menu);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(String ids) {
        try {
            String[] idArray = ids.split(",");
            menuService.deleteMenu(idArray);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 获取菜单树
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "menuTree", method = RequestMethod.GET)
    public String menuTree(HttpServletRequest request) {
        try {
            return toJsonListData(menuService.menuTree(null));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }


    /**
     * 获取左侧菜单栏--根据当前用户
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMenu", method = RequestMethod.GET)
    public String getMenu(HttpServletRequest request) {
        try {
            return toJsonListData(menuService.getUserMenu(GlobalValue.NO));
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 上移下移排序
     *
     * @param request
     * @param type    1上移 2下移
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "changeSort", method = RequestMethod.POST)
    public String changeSort(HttpServletRequest request, Integer type, Integer id) {
        try {
            menuService.changeSort(id, type);
            return toJsonSuccessData();
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 获取用户拥有的当前页面的所有权限
     * @param pageModel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "curPagePermission", method = RequestMethod.GET)
    public String curPagePermission(HttpServletRequest request,String pageModel){
        request.setAttribute("permission",menuService.curPagePermission(pageModel));
        return toJsonSuccessData();
    }

}
